package com.web.server.common.utils;

import com.google.gson.Gson;
import jodd.http.HttpRequest;
import jodd.http.HttpResponse;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Created by Hack on 2016/11/7.
 * 发送短信辅助类
 */
public class SmsUtil {
    private static Logger RUN_LOG = LoggerFactory.getLogger(SmsUtil.class);
    private static String DEFAULT_SMS_SERVER_URL = "http://10.16.49.3:8765/smss/open/sendSms";
    private static final String SMS_SERVER_URL = StringUtils.isEmpty(ConfigUtil.getProperty("sms.url")) ? DEFAULT_SMS_SERVER_URL : ConfigUtil.getProperty("sms.url");

    public static void send(String telephone, String message) {
        Sms sms = new Sms(telephone, message);
        RUN_LOG.info(String.format("Send-Sms-Request: [url=%s, telephone=%s, message=%s, vote=%s, ext=%s]",
                SMS_SERVER_URL, sms.desNo, sms.msg, sms.account, sms.ext));

        HttpResponse response = HttpRequest.post(SMS_SERVER_URL).bodyText(new Gson().toJson(new Sms(telephone, message)), "application/json", "UTF-8").send();
        RUN_LOG.info(String.format("Send-Sms-Response: [response=%s]", response.bodyText()));
    }

    private static class Sms {
        private String account; // 标注发送短息的组
        private String desNo;   // 手机号码，多个号码用“，”分隔
        private String msg;     // 内容
        private String ext;     // 国内指定为 1，国外是什么未明确

        public Sms(String desNo, String msg) {
            this.ext = "1";
            this.msg = msg;
            this.desNo = desNo;
        }

        public String getAccount() {
            return account;
        }

        public void setAccount(String account) {
            this.account = account;
        }

        public String getDesNo() {
            return desNo;
        }

        public void setDesNo(String desNo) {
            this.desNo = desNo;
        }

        public String getMsg() {
            return msg;
        }

        public void setMsg(String msg) {
            this.msg = msg;
        }

        public String getExt() {
            return ext;
        }

        public void setExt(String ext) {
            this.ext = ext;
        }
    }

    public static void main(String[] args) {
        SmsUtil.send("18513418598", "祈同学，你能收到我的信息吗？ \n----显良宝宝");
    }
}
